package com.jinzhi.jzweb.controller;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

import com.jinzhi.common.utils.QrCodeUtil;
import com.jinzhi.jzweb.dao.InfoStoreDao;
import com.jinzhi.oss.service.OssService;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.InfoStoreDO;
import com.jinzhi.jzweb.service.InfoStoreService;
import com.jinzhi.sys.service.UserService;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.service.DictService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-02-02 14:44:35 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/infoStore")
public class InfoStoreController extends BaseController {
	@Autowired
	private InfoStoreService infoStoreService;
	@Autowired
	private UserService userService;
	@Autowired
	private OssService ossService;
	@Autowired
	private DictService dictService;

	@GetMapping()
	@RequiresPermissions("jzweb:infoStore:infoStore")
	String InfoStore(){
	    return "jzweb/infoStore/infoStore";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:infoStore:infoStore")
	public Result<Page<InfoStoreDO>> list(InfoStoreDO infoStoreDTO){
		Wrapper<InfoStoreDO> wrapper = new EntityWrapper<InfoStoreDO>().orderBy("id",false);
		if(infoStoreDTO.getName()!=null){
			wrapper.like("name",infoStoreDTO.getName());
		}
		// 查询列表数据
		Page<InfoStoreDO> page = infoStoreService.selectPage(getPage(InfoStoreDO.class),wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				InfoStoreDO obj = (InfoStoreDO) page.getRecords().get(i);
				obj.setCreaterName(userService.selectById(obj.getCreaterId()).getName());
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:infoStore:add")
	String add(){
	    return "jzweb/infoStore/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:infoStore:edit")
	String edit(@PathVariable("id") Long id,Model model){
		InfoStoreDO infoStore = infoStoreService.selectById(id);
		model.addAttribute("infoStore", infoStore);
	    return "jzweb/infoStore/edit";
	}

	@GetMapping("/openErCode/{id}")
	@RequiresPermissions("jzweb:infoStore:edit")
	String openErCode(@PathVariable("id") String id,Model model){
		InfoStoreDO infoStoreDO = infoStoreService.selectById(id);
		model.addAttribute("erCode", infoStoreDO.getErCode());
		return "jzweb/infoStore/openErCode";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:infoStore:add")
	public Result<String> save( InfoStoreDO infoStore) throws IOException {
		infoStore.setStatus(0);
		infoStore.setCreaterId(this.getUserId());
		infoStore.setCreaterDate(new Date());
		infoStore.setMultiple(0);
		infoStoreService.createInfoStore(infoStore);
		String url="http://www.jz1x.com/certificate/infoMain?infoId="+infoStore.getId();
		//String url="http://www.jz1x.com";
		File file=QrCodeUtil.createQrCode(url,"C:/erCode/",infoStore.getId()+"二维码.jpg");
		FileInputStream input = new FileInputStream(file);
		MultipartFile multipartFile = new MockMultipartFile("file",file.getName(),"text/plain", IOUtils.toByteArray(input));
		String erCodeUrl=ossService.uploadFileOss(multipartFile);
		infoStore.setUrl(url);
		infoStore.setErCode(erCodeUrl);
		infoStoreService.updateById(infoStore);
        return Result.ok();
	}
	
	@Log("重复提交开关")
	@ResponseBody
	@PostMapping("/changeMultiple")
	public Result<String> changeMultiple( Long id,String cmd){
		InfoStoreDO infoStore = infoStoreService.selectById(id);
		if(cmd.equals("start"))infoStore.setMultiple(1);
		else infoStore.setMultiple(0);
		infoStoreService.updateById(infoStore);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:infoStore:edit")
	public Result<String>  update( InfoStoreDO infoStore) throws IOException {
		InfoStoreDO info = infoStoreService.selectById(infoStore.getId());
		if(!info.getUrl().equals(infoStore.getUrl())){
			String logo="D://upload/logo.jpg";
			File file=QrCodeUtil.createQrCodeLogo(infoStore.getUrl(),"C:/erCode/",infoStore.getId()+"二维码.jpg",logo);
			FileInputStream input = new FileInputStream(file);
			MultipartFile multipartFile = new MockMultipartFile("file",file.getName(),"text/plain", IOUtils.toByteArray(input));
			String erCodeUrl=ossService.uploadFileOss(multipartFile);
			infoStore.setErCode(erCodeUrl);
		}
		boolean update = infoStoreService.updateById(infoStore);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:infoStore:remove")
	public Result<String>  remove( Long id){
		infoStoreService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:infoStore:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		infoStoreService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	@Log("生成二维码")
	@PostMapping( "/erCodeCreate")
	@ResponseBody
	@RequiresPermissions("jzweb:infoStore:infoStore")
	public Result<String>  erCodeCreate(String url){
		return Result.ok();
	}
	
}
